
class jQuery {
    constructor(selector) {
        const result = document.querySelectorAll(selector);
        const length = result.length;
        for (let i = 0; i < length; i++) {
            this[i] = result[i];
        }
        this.length = length;
        this.selector = selector;
        //类似于数组，实际上是对象
    }
    get(index) {
        return this[index]
    }
    each(fn) {
        for (let i = 0; i < this.length; i++) {
            const elem = this[i];
            fn(elem)
        }
    }
    on(type, fn) {
        return this.each(elem => {
            elem.addEventListener(type, fn, false)
        })
    }
    //还可以扩展很多DOM操作的API
}

    // 插件
    jQuery.prototype.dialog = function (info) {
    alert(info)
}

// "造轮子"
class myJquery extends jQuery {
    constructor(selector) {
        super(selector)
    }
    // 扩展自己的方法
    addClass() { }
    style() { }
}
// 使用 自制版的jQuery
const _p = new jQuery('p');
// console.log($p);
console.log(_p.get(2));; // 得到 <p>一段文字3</p>
_p.each(elem => console.log(elem.nodeName)) // 3个p

_p.on('click', () => { alert(clicked) })//弹出 clicked

